﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using FrameWork;
using FrameWork.Components;
using FrameWork.WebControls;
using FrameWorkWeb;


namespace FrameWork.web.Seach
{
    public partial class ImportStorage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ArrayList lst = BusinessFacade.sys_UserRolesDisp(Common.Get_UserID);
                string strRoleID = "-1";
                if (lst.Count != 0)
                {
                    strRoleID = ((sys_UserRolesTable)lst[0]).R_RoleID.ToString();
                    SetHeadText(strRoleID);
                }

                AllSeach _AllSeach = new AllSeach();
                //绑定货主
                DataTable dtCustomer = _AllSeach.SupesoftPage("wUser_HZSeach", "*", int.MaxValue - 1, 1, " where 1=1 and userID=" + Common.Get_UserID.ToString(), "code", "0", true).Tables[0];

                ddlCustom.DataValueField = "code";
                ddlCustom.DataTextField = "chnname";
                ddlCustom.DataSource = dtCustomer;
                ddlCustom.DataBind();
                //绑定仓库
                DataTable dtStorage = _AllSeach.SupesoftPage("vStorageWEB", "*", int.MaxValue - 1, 1, " where 1=1", "v_code", "0", false).Tables[0];
                ddlStorages.DataValueField = "v_text";
                ddlStorages.DataTextField = "v_code";
                ddlStorages.DataSource = dtStorage;
                ddlStorages.DataBind();
                //绑定入库状态
                DataTable dtInstocktype = _AllSeach.SupesoftPage("v_StockINTypeWeb", "*", int.MaxValue - 1, 1, " where 1=1", "Code", "0", true).Tables[0];
                ddlinstocktype.DataValueField = "NAME";
                ddlinstocktype.DataTextField = "NAME";
                ddlinstocktype.DataSource = dtInstocktype;
                ddlinstocktype.SelectedValue = "全部";
                ddlinstocktype.DataBind();
            }
        }

        private void BindData(int intPageIndex)
        {  
            AllSeach _AllSeach = new AllSeach();
            DataSet dtStockIN = _AllSeach.SupesoftPage("vStockINWEB", "*", Common.PageSize, intPageIndex, this.Button1.CommandArgument.ToString(), "stockinno", "1", true);
            
            this.GridView1.DataSource = dtStockIN.Tables[0];
            this.GridView1.DataBind();
            this.AspNetPager1.RecordCount = int.Parse(dtStockIN.Tables[1].Rows[0][1].ToString());
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            TabOptionWebControls1.SelectIndex = 1;
            string strMeg = "where 1=1 ";
            
            strMeg += " AND Customer ='" + this.ddlCustom.SelectedValue + "'"; 
            
            if (this.txtLH.Text.Trim()!="")
            {
                strMeg += " AND SKU like '%" + this.txtLH.Text.Trim() + "%'"; 
            }
            
            if (this.txtStart.Text.Trim() != "")
            {
                strMeg += " AND CreateDate >='" + this.txtStart.Text.Trim() + " 00:00:01' ";
            }
            else
            {
                DateTime begTime;
                begTime = DateTime.Now.AddDays(-30);
                strMeg += " AND CreateDate >='" + begTime.ToString("yyyy-MM-dd") + " 00:00:01' "; 
            }

            if (this.txtEnd.Text.Trim() != "")
            {
                strMeg += " AND CreateDate <='" + this.txtEnd.Text.Trim() + " 23:59:59' ";
            }
            else
            {
                strMeg += " AND CreateDate <='" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59' "; 
            }

            if (this.ddlinstocktype.SelectedValue!="全部")
            {
                strMeg += " AND StockINType ='" + this.ddlinstocktype.SelectedValue + "'"; 
            }
            
            this.Button1.CommandArgument = strMeg;
            BindData(1);
        }

        private void SetHeadText(string strRoleID)
        {
            if (this.labHeadText.Text == null || this.labHeadText.Text == "")
            {
                for (int i = 0; i < GridView1.Columns.Count; i++)
                {
                    this.labHeadText.Text += GridView1.Columns[i].HeaderText + ",";
                }
                this.labHeadText.Text = this.labHeadText.Text.Substring(0, this.labHeadText.Text.Length - 1);
            }

            AllSeach _AllSeach = new AllSeach();
            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();
            if (strRoleID != "-1")
            {
                string str = BusinessFacade.sys_RolesDisp(int.Parse(strRoleID)).WColConfigID;
                dt1 = _AllSeach.SupesoftPage("vColConfig", "*", int.MaxValue - 1, 1, " where ID=" + str, "ID", "1", false).Tables[0];
                dt2 = _AllSeach.SupesoftPage("vStockINWEB", "*", int.MaxValue - 1, 1, " where 1=2", "Customer", "1", true).Tables[0];
            }

            string[] strHeadTexts = this.labHeadText.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < strHeadTexts.Length; i++)
            {
                GridView1.Columns[i].HeaderText = strHeadTexts[i];
            }

            List<DataColumn> listColDel = new List<DataColumn>();
            List<DataColumn> listColShow = new List<DataColumn>();

            foreach (DataColumn dc in dt2.Columns)
            {
                if (dc.ColumnName.ToLower().Contains("LotAtt".ToLower()))
                {
                    string strColName = dc.ColumnName;
                    DataRow[] drs = dt1.Select("ColDataName='" + strColName + "'");
                    if (drs.Length == 0)
                    {
                        listColDel.Add(dc);
                    }
                    else
                    {
                        dc.Caption = drs[0]["ColWebName"].ToString();
                        listColShow.Add(dc);
                    }
                }
            }

            foreach (DataColumn dc in listColDel)
            {
                for (int i = 0; i < GridView1.Columns.Count; i++)
                {
                    if (GridView1.Columns[i].HeaderText.ToLower() == dc.ColumnName.ToLower())
                    {
                        GridView1.Columns[i].Visible = false;
                    }
                }
            }

            foreach (DataColumn dc in listColShow)
            {
                for (int i = 0; i < GridView1.Columns.Count; i++)
                {
                    if (GridView1.Columns[i].HeaderText.ToLower() == dc.ColumnName.ToLower())
                    {
                        GridView1.Columns[i].HeaderText = dc.Caption;
                    }
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData(AspNetPager1.CurrentPageIndex);
        }

        protected void lbtnExport_Click_Click(object sender, EventArgs e)
        {
            AllSeach _AllSeach = new AllSeach();
            DataSet ds = _AllSeach.SupesoftPage("vStockINWEB", "Customer,StockINNo as 编号,StockINType as 订单类型,StockINStatusLine as 订单状态,StockINLineNo as 行号,custchnname as 发货单位,SKU as 产品,ChnName as 中文描述,CreateDate as 创建时间,customerorder as 客户订单号,billno as 发票号,ExpectedQty as 预期数量,ReceivedQty as 完收数量,lotatt01,lotatt02,lotatt03,lotatt04,lotatt05,lotatt06,lotatt07,lotatt08,lotatt09,lotatt10,lotatt11,lotatt12", int.MaxValue - 1, 1, this.Button1.CommandArgument.ToString(), "customer", "0", true);
            ds.Tables[0].Columns["Customer"].ColumnName = "客户";
            
            CreateExcel(ds, "StockINWEB.xls");
        }

        public void CreateExcel(DataSet ds, string FileName)
        {
            HttpResponse resp;
            resp = Page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文   
            resp.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。    
            Page.EnableViewState = false;


            string colHeaders = "", ls_item = "";

            //定义表对象与行对象，同时用DataSet对其值进行初始化 
            DataTable dt = ds.Tables[0];

            DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
            int i = 0;
            int cl = dt.Columns.Count;

            //取得数据表各列标题，各标题之间以t分割，最后一个列标题后加回车符 
            for (i = 0; i < cl; i++)
            {
                if (i == (cl - 1))//最后一列，加n
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\n";
                }
                else
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\t";
                }

            }
            resp.Write(colHeaders);
            //向HTTP输出流中写入取得的数据信息 

            //逐行处理数据   
            if (myRow.Length > 0)
            {
                foreach (DataRow row in myRow)
                {
                    //当前行数据写入HTTP输出流，并且置空ls_item以便下行数据     
                    for (i = 0; i < cl; i++)
                    {
                        if (i == (cl - 1))//最后一列，加n
                        {
                            ls_item += row[i].ToString() + "\n";resp.Write(ls_item);
                        }
                        else
                        {
                            ls_item += row[i].ToString() + "\t";
                        }
                        
                    }

                    ls_item = "";

                }
            }
            else
            {
                resp.Write(ls_item);
            }
            resp.Flush();
            resp.End();
        }

    }
}
